package com.cloud.cmt.usermanage.controller;

import com.angela.cloud.common.base.ResultMessage;
import com.angela.cloud.common.constant.ResultCodeConstant;
import com.cloud.cmt.usermanage.entity.RoleGroup;
import com.cloud.cmt.usermanage.entity.UserInfo;
import com.cloud.cmt.usermanage.po.TenantIdByUplinkPo;
import com.cloud.cmt.usermanage.service.RoleGroupRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * User: wcy
 * Date: 2017/10/17
 * Time: 16:14
 */
@RestController
@RequestMapping("/rolegroup")
@Api(value = "/角色管理", description = "角色管理-用户角色组增删查改等-by wcy")
public class RoleGroupCtl {

    @Resource
    RoleGroupRepository RoleGroupRepository;

    @ApiOperation(value = "通过id获取信息")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public RoleGroup getUser(@PathVariable long id){
        return (RoleGroup) RoleGroupRepository.findOne(id);
    }

    @ApiOperation(value = "保存信息")
    @PostMapping(value = "")
    @ResponseBody
    public ResultMessage<RoleGroup> save(@RequestBody RoleGroup RoleGroup){
        ResultMessage resultMessage = new ResultMessage();
        RoleGroup = RoleGroupRepository.save(RoleGroup);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        resultMessage.setId(RoleGroup.getId()+"");

        return resultMessage;
    }

    @ApiOperation(value = "更新信息")
    @PutMapping(value = "")
    @ResponseBody
    public ResultMessage<RoleGroup> update(@RequestBody RoleGroup RoleGroup){
        ResultMessage resultMessage = new ResultMessage();
        RoleGroupRepository.saveAndFlush(RoleGroup);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);

        return resultMessage;
    }

    @ApiOperation(value = "树查询，根据父节点id，和租户id获取子节点，顶级节点为-1")
    @PutMapping(value = "/findByTenantIdAndUplink")
    @ResponseBody
    public List<RoleGroup> findByTenantIdAndUplink(@RequestBody TenantIdByUplinkPo po){
        return RoleGroupRepository.findByTenantIdAndUplink(po.getTenantId(),po.getUplink());
    }

    @ApiOperation(value = "通过id删除")
    @DeleteMapping(value = "/{id}")
    public ResultMessage remove(@PathVariable("id") Long id) {
        ResultMessage resultMessage = new ResultMessage();
        RoleGroupRepository.delete(id);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        return  resultMessage;
    }

}
